/* transport 角色处理任务 */
export default class CreepTransport extends Creep {
    /* 处理虫卵填充任务 */
    public handleFTE():void{
        if (!this.room.memory.TaskMemory.storageID) return
        var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
        if (! storage_) return
        this.workstate('energy')
        if (this.memory.working)
        {
            this.say("👨‍🍼")
            var extensions = this.pos.findClosestByPath(FIND_STRUCTURES,{filter:(structure)=>{
                return (structure.structureType == 'spawn' || structure.structureType == 'extension') && structure.store.getFreeCapacity('energy') > 0
            }})
            if (extensions)
            this.transfer__(extensions,'energy')
            else
            Game.rooms[this.memory.belong].deleteMisson('CREEP','虫卵填充')
        }
        else
        {
            if (storage_.store['energy'] >= this.store.getCapacity())
            this.withdraw__(storage_,'energy')
            else Game.rooms[this.memory.belong].deleteMisson('CREEP','虫卵填充')
            this.wipeResource('energy',storage_)
        }
        this.wipeResource('energy',storage_)
    }

    /* 处理远塔填充任务 */
    public handleSToFT():void{
        this.workstate('energy')
        if (this.memory.working)
        {
            this.say('仓->塔')
            for (var i of this.room.memory.TaskMemory.OtowerID)
            {
                var tower_ = Game.getObjectById(i) as StructureTower
                if (tower_.store.getFreeCapacity('energy') > 200)
                {
                    this.transfer__(tower_,'energy')
                    return
                }
            }
            this.room.deleteMisson("CREEP",'远塔填充')
        }
        else
        {
            var storage_ = Game.getObjectById(this.room.memory.TaskMemory.storageID as string) as StructureStorage
            if (!storage_) return
            this.withdraw__(storage_,'energy')
            
        }
        this.wipeResource('energy',storage_)
    }

    /* 处理物流搬运任务 允许多任务，允许多工种 */
    public handleLT():void{
        var thisTask = Game.rooms[this.memory.belong].getMisson_S('CREEP','物流搬运',this.memory.role)
        var sou = Game.getObjectById(thisTask.sourceStructure[0]) as StructureStorage | StructureTerminal | StructureLink | StructureLab
        var dis = Game.getObjectById(thisTask.disStructure[0]) as StructureStorage | StructureTerminal | StructureLink | StructureLab | StructurePowerSpawn | StructureTower | StructureNuker
        var rType = thisTask.relateResource[0]
        this.workstate(rType)
        this.say(`🚚${rType}`)
        if (this.memory.working)
        {
            if (thisTask.disnum != null && thisTask.sourcenum == null)
            {
                if (dis.store[rType] >= thisTask.disnum)
                {
                    Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID)
                    return
                }
            }
            this.transfer__(dis,rType)
        }
        else
        {
            if (thisTask.sourcenum != null && thisTask.disnum == null)
            {
                if (sou.store[rType] <= thisTask.sourcenum || !sou.store[rType])
                {
                    Game.rooms[this.memory.belong].deleteMisson_ID(thisTask.ID)
                    return
                }
            }
            this.withdraw__(sou,rType)
        }
        /* 清除无关资源 */
        for (var s in this.store)
        {
            if (s != rType)
            {
                var storage_ = Game.getObjectById( this.room.memory.TaskMemory.storageID as string) as StructureStorage
                this.transfer__(storage_,s as ResourceConstant)
            }
        }
        this.LastMisson(10)
    }
}